package com.example.jdbcdemo.service.Impl;

import com.example.jdbcdemo.mapper.GoodsMapper;
import com.example.jdbcdemo.mapper.ProductMapper;
import com.example.jdbcdemo.mapper.StorageMapper;
import com.example.jdbcdemo.pojo.Goods;
import com.example.jdbcdemo.pojo.Product;
import com.example.jdbcdemo.pojo.Storage;
import com.example.jdbcdemo.service.StatsStorageServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class StatsStorageServiceImpl implements StatsStorageServiceInterface {
    @Autowired
    StorageMapper storageMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Map<String,Object>> statsNumCost() {
        List<Storage> storageList= storageMapper.queryStorageList();//获取仓库信息列表
        List<Map<String,Object>> resultList=new LinkedList<>();
        //遍历每一个仓库
        for (Storage storage:storageList){
            Map<String,Object> tempMap=new LinkedHashMap<>();
            tempMap.put("storageId",storage.getId());
            tempMap.put("storageAddres",storage.getAddress());
            //获得该仓库下的所有库存信息
            List<Goods> goodsList=goodsMapper.queryGoodsListByStorageId(storage.getId());
            List<Map<String,Object>> tempGoodsList=new LinkedList<>();//每个仓库的tempMap中都有一个list存放多个货物信息
            for (Goods goods:goodsList){
                int productId=goods.getProductId();//获取商品id
                Product product=productMapper.queryProductById(productId);
                Map<String,Object>temptempMap=new LinkedHashMap<>();
                temptempMap.put("productId",productId);
                temptempMap.put("productName",product.getName());
                double cost=product.getCost();
                int num=goods.getNumber();
                temptempMap.put("productCost",cost);//商品成本
                temptempMap.put("goodsNum",num);//库存数量
                //每个货品的积压资金额=商品成本*库存数量
                temptempMap.put("backlogOfFunds",cost*num);
                tempGoodsList.add(temptempMap);
            }
            tempMap.put("goodsMessage",tempGoodsList);
            resultList.add(tempMap);
        }
        return resultList;
    }
}
